package mysql

import (
	"manuscript-service/manrpc/manrpc"

	"gorm.io/gorm"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);not null;commit:'稿件标题'"`
	Like    int64  `gorm:"type:int(11);not null;commit:'稿件点赞'"`
	Content string `gorm:"type:varchar(255);not null;commit:'稿件评论'"`
	Info    string `gorm:"type:varchar(255);not null;commit:'稿件内容'"`
	CateId  int64  `gorm:"type:int(11);not null;commit:'类别id'"`
}

func (m *Manuscript) ManAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Manuscript) ManFind(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(m).Error
}

func (m *Manuscript) List(db *gorm.DB, lists Manuscript) error {
	return db.Debug().Find(lists).Error
}

func ManList(db *gorm.DB, list []*manrpc.ListsMan) []*manrpc.ListsMan {
	db.Debug().Model(&Manuscript{}).Select("manuscripts.id,manuscripts.title,manuscripts.like,manuscripts.info,manuscripts.content,categories.cate_name").
		Joins("JOIN categories ON manuscripts.cate_id =categories.id").Find(&list)
	return list
}
